//
//  LoginViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-14.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "FindPasswordViewController.h"
#define USER_NAME_FIELD_TAG 1001
#define USER_PASSWORD_FIELD_TAG 1002

@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    self.navigationItem.rightBarButtonItem = rightNavButton(self, @selector(rightBtAction:),@"忘记密码");
    self.userNameField.inputAccessoryView = [self getInputAccessoryView];
    self.userNameField.tag = USER_NAME_FIELD_TAG;
    self.userNameField.delegate = self;
    self.userPasswordField.inputAccessoryView = [self getInputAccessoryView];
    self.userPasswordField.tag = USER_PASSWORD_FIELD_TAG;
    self.userPasswordField.delegate = self;
    [self.autoLoginBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
    [self.registerBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
    //检查自动登录的标识
    NSString *autoLoginFlag = [DataStoreService loadFromKeychain:AUTO_LOGIN_FLAG];
    if (autoLoginFlag && [autoLoginFlag isEqualToString:AUTO_LOGIN_ENABLE]) {
        self.autoLoginCheckBoxImage.image = [UIImage imageNamed:@"checkbox.png"];
    } else {
        self.autoLoginCheckBoxImage.image = [UIImage imageNamed:@"uncheckbox.png"];
    }
    [UserInfoService getUserInfoService].delegate = self;
    if (RunTimeOSVersion >= 7.0) {

//        self.navigationController.navigationBarHidden = YES;
    }


}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"search_bg_v2.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Button Methods
-(void)rightBtAction:(id)sender
{
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"忘记密码正在建设中..." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alert show];
    FindPasswordViewController *findPassVC = [[FindPasswordViewController alloc]initWithNibName:@"FindPasswordViewController" bundle:nil];
    findPassVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:findPassVC animated:YES];
    
}

- (IBAction)loginBtnAction:(id)sender
{
    
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"successLoginWithIndexViewController" object:nil];
//
//    return;
    NSString *userName = self.userNameField.text;
    NSString *userPass = self.userPasswordField.text;
    if (!userName || userName.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (!userPass || userPass.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    //调用登录
    [self startLoadingViewWithText:@"正在登录..."];
    [[UserInfoService getUserInfoService] checkUserLoginWithName:userName andPassword:userPass];
}

- (IBAction)autoLoginBtnAction:(id)sender
{
    NSString *autoLoginFlag = [DataStoreService loadFromKeychain:AUTO_LOGIN_FLAG];
    if (autoLoginFlag && [autoLoginFlag isEqualToString:AUTO_LOGIN_ENABLE]) {
        //之前选的是自动登录
        self.autoLoginCheckBoxImage.image = [UIImage imageNamed:@"uncheckbox.png"];
        [DataStoreService saveToKeychain:AUTO_LOGIN_FLAG data:AUTO_LOGIN_DISABLE];
    } else {
        //之前选的是不自动登录
        self.autoLoginCheckBoxImage.image = [UIImage imageNamed:@"checkbox.png"];
        [DataStoreService saveToKeychain:AUTO_LOGIN_FLAG data:AUTO_LOGIN_ENABLE];
    }
}

- (IBAction)registerBtnAction:(id)sender {
    RegisterViewController *registerVC = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
    registerVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark - Text Field Methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.tag) {
        case USER_NAME_FIELD_TAG:
            [self.userPasswordField becomeFirstResponder];
            break;
        case USER_PASSWORD_FIELD_TAG:
            [self loginBtnAction:self.loginBtn];
            break;
        default:
            break;
    }
    return YES;
}
#pragma mark - User Info Service Methods
-(void)loadUserInfoFinished:(id)result
{
    [self stopLoadingView];
    [[UserInfoService getUserInfoService] loginSuccessHandler:result];
    [self.navigationController popViewControllerAnimated:YES];
    NSString *autoLoginFlag = [DataStoreService loadFromKeychain:AUTO_LOGIN_FLAG];
    if (autoLoginFlag && [autoLoginFlag isEqualToString:AUTO_LOGIN_ENABLE]) {
        //勾选了自动登录，则在这里保存密码
        [DataStoreService saveToKeychain:AUTO_LOGIN_USER_NAME data:self.userNameField.text];
        [DataStoreService saveToKeychain:AUTO_LOGIN_USER_PASSWORD data:self.userPasswordField.text];
    } else {
        //没勾选自动登录，尝试清除数据
        [DataStoreService deleteFromKeychain:AUTO_LOGIN_USER_NAME];
        [DataStoreService deleteFromKeychain:AUTO_LOGIN_USER_PASSWORD];
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"successLoginWithIndexViewController" object:nil];
}

-(void)loadUserInfoFaild:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [self stopLoadingView];
}
@end
